package cn.jcby.app.lebu.service.impl;

import cn.jcby.app.config.QiNiuConfig;
import cn.jcby.app.lebu.controller.req.BasicListReq;
import cn.jcby.app.lebu.controller.res.UploadFilesRes;
import cn.jcby.app.lebu.dao.UserAlbumDao;
import cn.jcby.app.lebu.dao.UserDao;
import cn.jcby.app.lebu.entity.UserAlbumEntity;
import cn.jcby.app.lebu.entity.UserEntity;
import cn.jcby.app.lebu.service.QiNiuService;
import cn.jcby.app.lebu.service.UserAlbumService;
import cn.jcby.app.lebu.utils.PageUtils;
import cn.jcby.app.lebu.utils.UserInfoUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service("userAlbumService")
public class UserAlbumServiceImpl extends ServiceImpl<UserAlbumDao, UserAlbumEntity> implements UserAlbumService {

    @Autowired
    private QiNiuConfig qiuConfig;
    @Autowired
    private QiNiuService qiNiuService;
    @Autowired
    private UserAlbumDao userAlbumDao;
    @Autowired
    private UserDao userDao ;

    @Override
    public PageUtils queryPage(BasicListReq params) {
        IPage<UserAlbumEntity> page = this.page(
                new Page<>(params.getPage(),params.getLimit()),
                Wrappers.<UserAlbumEntity>lambdaQuery()
        );

        return new PageUtils(page);
    }
    @Override
    public void add(UserAlbumEntity userAlbumReq){
        UserAlbumEntity userAlbum = new UserAlbumEntity() ;
        save(userAlbum) ;
    }

    @Override
    public void updateBy(UserAlbumEntity userAlbumReq){
        UserAlbumEntity userAlbum = new UserAlbumEntity() ;
        updateById(userAlbum) ;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<UploadFilesRes> uploadFiles(MultipartFile[] files) {
        List<UploadFilesRes> retList = new ArrayList<>();
        //文件上传到七牛云
        if (files!= null && files.length > 0) {
            List<String> facesList = qiNiuService.uploadFiles(files, "album");
            //保存到数据库
            List<UserAlbumEntity> saveList = new ArrayList<>();
            for (String faceUrl : facesList) {
                UserAlbumEntity userAlbum = new UserAlbumEntity();
                userAlbum.setImageUrl(faceUrl);
                userAlbum.setUserId(UserInfoUtil.getUserId());
                userAlbum.setFace(0);
                userAlbum.setDel(0);
                saveList.add(userAlbum);
                UploadFilesRes res = new UploadFilesRes();
                res.setFace(0);
                res.setImageUrl(qiuConfig.getDomain()+"/" +faceUrl);
                retList.add(res) ;
            }
            if (CollectionUtils.isNotEmpty(saveList)){
                saveBatch(saveList);
            }
        }
        return retList;
    }

    @Override
    public UserAlbumEntity infoById(String id) {
        return userAlbumDao.selectOne(Wrappers.<UserAlbumEntity>lambdaQuery()
                .eq(UserAlbumEntity::getId,id)
                .eq(UserAlbumEntity::getUserId,UserInfoUtil.getUserId())
        ) ;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setAvatar(String id) {
        UserAlbumEntity entity = getById(id);
        //清除头像
       /* update(Wrappers.<UserAlbumEntity>lambdaUpdate()
                .set(UserAlbumEntity::getFace,0)
                .ne(UserAlbumEntity::getId,id)
                .eq(UserAlbumEntity::getUserId,UserInfoUtil.getUserId())
        ) ;
        update(Wrappers.<UserAlbumEntity>lambdaUpdate()
               .set(UserAlbumEntity::getFace,1)
               .eq(UserAlbumEntity::getId,id)) ;*/

        userDao.updateById(UserEntity.builder()
                .faceUrl(entity.getImageUrl())
                .id(UserInfoUtil.getUserId())
                .build()
        ) ;
    }
}